package com.whut.gkd.controller;


import com.whut.gkd.bean.Good;
import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.bean.User;
import com.whut.gkd.service.IGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("good")
public class GoodController {

    @Autowired
    IGoodService goodService;




    @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage, String goodName,
                                @RequestParam(defaultValue = "0") int type ,
                                HttpSession session){
        if(type == 1){
            session.setAttribute("searchGoodName",goodName);
        }else if(type == 0){
            goodName = (String) session.getAttribute("searchGoodName");
        }else if(type == 2){
            //if(session.getAttribute("searchGoodName") != null){
                session.removeAttribute("searchGoodName");
            //}
        }
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        ModelAndView mv = new ModelAndView();
        //if()
        PageInfo pageInfo = goodService.findAll(currentPage,goodName, shopinfo.getShopId());
        mv.setViewName("good-list");
        mv.addObject("pageInfo",pageInfo);

        return mv;
    }

    @RequestMapping("toUpdateGood.do")
    public ModelAndView toUpadte(int goodId, HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        int shopId = shopinfo.getShopId();
        Good good = goodService.selectGoodById(goodId, shopId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("good",good);
        mv.setViewName("good-update");
        return mv;
    }

    @RequestMapping("updateGood.do")
    public String update(Good good, HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        int shopId = shopinfo.getShopId();
        Integer integer = new Integer(shopId);

        goodService.update(good, integer.intValue());
        return "redirect:findAll.do";
    }


    @RequestMapping("deleteById.do")
    public String deleteById(int goodId, HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        int shopId = shopinfo.getShopId();
        goodService.deleteById(goodId, shopId);
        return "redirect:findAll.do";
    }

    @RequestMapping("deleteSelectedById.do")
    public String deleteSelectedById(String ids, HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        int shopId = shopinfo.getShopId();
        String[] strs = ids.split(",");
        List<Integer> idsList = new ArrayList<>();
        for(String s:strs){
            goodService.deleteById(Integer.parseInt(s),shopId);
        }
        return "redirect:findAll.do";
    }

}
